/*
    curlplusplus - a c++ wrapper around libcurl
    Copyright (C) 2012  Francesco "Franc[e]sco" Noferi (francesco1149@gmail.com)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the
    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
    Boston, MA  02110-1301, USA.
*/

#pragma once

#include "curlpp.hpp"

namespace curlpp
{
    /**
     * Wraps libcurl's share interface.
     */
    class share
    {
    public:
        /**
         * Default constructor.
         *
         * @throw curlpp::exception an exception that describes the general cause of the error.
         */
        share() throw(const exception &);

        /**
         * Default destructor.
         *
         * @throw curlpp::exception an exception that describes the general cause of the error.
         */
        virtual ~share() throw(const exception &);

        /**
         * Sets a shared object option.
         * See <a href="http://curl.haxx.se/libcurl/c/curl_share_setopt.html">curl_share_setopt</a> for more info
         * about the available options.
         *
         * @param tag the option to set.
         * @param value the value to assign to the option.
         *
         * @throw curlpp::exception an exception that describes the general cause of the error.
         */
        template <typename T>
        void setopt(CURLSHoption tag, T value) throw(const exception &)
        {
            attempt(curl_share_setopt(curl, tag, value), "share.setopt: failed to set option - ");
        }

    protected:
        CURLSH *curl; /**< Internal cURL share handle */

        /**
         * If the passed CURLSHcode represents an error, throws an exception that describes
         * the error, otherwise does nothing.
         *
         * @param res the error code returned by a curl_share function.
         * @param errortext a general description of the error. The curl error string will
         *                  be appended to this string.
         * @throw curlpp::exception an exception that describes the general cause of the error.
         */
        void attempt(CURLSHcode res, std::string errortext) throw(const exception &);
    };
}
